package com.dxy.hms.socketio;

import cn.hutool.core.lang.Assert;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOClient;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SocketIoSession implements ISession {

    private SocketIOClient socketIOClient;
    private Long createTime;

    public SocketIoSession(SocketIOClient socketIOClient) {
        Assert.notNull(socketIOClient, "socketIOClient不能为空");
        this.socketIOClient = socketIOClient;
        this.createTime = System.currentTimeMillis();
    }

    @Override
    public boolean isActive() {
        return socketIOClient.isChannelOpen();
    }

    @Override
    public void close() {
        socketIOClient.disconnect();
    }

    @Override
    public String getRemoteIp() {
        return socketIOClient.getRemoteAddress().toString();
    }

    @Override
    public String getRemotePort() {
        return socketIOClient.getRemoteAddress().toString();
    }

    @Override
    public String getSessionId() {
        return socketIOClient.getSessionId().toString();
    }

    @Override
    public String getConnectParam(String key) {
        HandshakeData handshakeData = socketIOClient.getHandshakeData();
        return handshakeData.getSingleUrlParam(key);
    }

    @Override
    public Long getCreateTime() {
        return this.createTime;
    }

    @Override
    public void sendMessage(String event, Object payload) {
        socketIOClient.sendEvent(event, payload);
    }
}
